package com.ysm.smfu.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.WriterException;
import com.ysm.smfu.R;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.utils.MyImageBitmapUtil;
import com.ysm.smfu.utils.ToastUtils;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.welcome.SharedConfig;
import com.ysm.smfu.zxing.encoding.EncodingHandler;
import com.zhy.autolayout.AutoLinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/10/14.
 */
public class PaySetActivityAgain extends BaseActivity {

    @BindView(R.id.consumer_qr)
    ImageView consumerQr;
    @BindView(R.id.saveToLoc)
    AutoLinearLayout saveToLoc;
    @BindView(R.id.consumer_setcount)
    TextView consumerSetcount;

    @BindView(R.id.qr_money)
    TextView qr_money;
    @BindView(R.id.clearQr)
    TextView clearQr;
    @BindView(R.id.title_body)
    TextView titleBody;
    @BindView(R.id.mainActivity_topbar_img)
    ImageView mainActivityTopbarImg;
    private String money;
    private Bitmap qrCode;
    private Intent intent;
    private String name;
    private String remark;
    private String pay_businessId;

    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.fragment_consumer_qr);
        ButterKnife.bind(this);
        consumerSetcount.setVisibility(View.GONE);
        clearQr.setVisibility(View.VISIBLE);

        intent = new Intent();
        intent.setAction("finish_paysetActivity");

        titleBody.setText("生成消费订单");
        mainActivityTopbarImg.setVisibility(View.GONE);

        SharedPreferences sp = new SharedConfig(UIUtils.getContext()).getCommoneSp();
        name = sp.getString(SpKey.NAME, "");
        SharedPreferences currShared = new SharedConfig(UIUtils.getContext(), name).getPrivateSp();
//        获取金额
        Intent intent = getIntent();
        money = intent.getStringExtra(SpKey.MONEY);
        remark = intent.getStringExtra(SpKey.REMARK);
        pay_businessId = intent.getStringExtra(SpKey.Pay_BUSSINESSID);

//        获取本地保存的商户名
        String shanghuName = currShared.getString(SpKey.SHANGHUNAME, "");
        if (TextUtils.isEmpty(shanghuName)) {
            ToastUtils.showToast("无法获取商户名，请重新登录");
            return;
        }
        // 获取二维码
        String jifen = "";
        try {
            qrCode = EncodingHandler.createQRCode(SpKey.QRFUKUAN + pay_businessId + "#" + money + "#" + jifen + "#" + remark, 400, 400, null);
            consumerQr.setImageBitmap(qrCode);
            qr_money.setText("￥" + money);
        } catch (WriterException e) {
//            生成二维码失败
            AlertDialog dialog = new AlertDialog.Builder(this)
                    .setTitle("提示")
                    .setMessage("账单二维码生成失败,请重试")
                    .setNegativeButton("确定", null)
                    .create();
            dialog.show();
        }
    }


    @OnClick({R.id.saveToLoc, R.id.consumer_setcount, R.id.clearQr, R.id.back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.saveToLoc:
                saveToLoc();
                break;
            case R.id.clearQr:
                clearQr();
                break;
            case R.id.back:
//                生成订单之后没有回头路，跳往选择积分或金额账单
                goBack();
                break;
        }
    }

    /**
     * 清除订单
     */
    private void clearQr() {
        goBack();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        goBack();
    }

    private void goBack() {
        sendBroadcast(intent);
        UIUtils.startActivity(PayActivity.class, "pay", 2);
        finish();
    }

    /**
     * 保存到本地并显示到图库中
     */
    private void saveToLoc() {
        MyImageBitmapUtil.saveQrToLoc(this, qrCode);
    }
}
